{% extends "../../base.html" %}

{% block crumbs %}
<div class="row">
    <div class="col-md-12">
        <ol class="breadcrumb">
            <li>
                <a href="/yqjr/index">主页</a>
            </li>
            <li class="active">
                <a class="this">基础信息</a>
            </li>
            <li class="active">
                <a class="this-page">用例管理</a>
            </li>
        </ol>

    </div>
</div>
{% endblock %}

{% block body %}

<div class="row">
    <div class="col-md-12">
        <div class="panel">
            <div class="panel-heading">
                <a href="/base/case_add/" class="btn btn-primary">添加用例</a>
            </div>
            <div class="panel-body">
                <table class="table table-bordered">
                    <thead>
                    <tr>
                        <th>ID</th>
                        <th>用例名称</th>
                        <th>所属项目</th>
                        <th>描述</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for case in case_list %}
                    <tr>
                        <td>{{ case.case_id }}</td>
                        <td>{{ case.case_name }}</td>
                        <td prj_id="{{ case.project.prj_id }}">{{ case.project }}</td>
                        <td>{{ case.description }}</td>
                        <td>
                            <button class="#runCaseForm btn btn-primary" onclick="debug_button(this);" data-toggle="modal" data-target="#runCaseForm">运行</button>
                            <a href="" class="">编辑</a>
                            <a href="" class="">删除</a>
                        </td>
                    </tr>
                    {% endfor %}
                    </tbody>
                </table>
                <div class="modal fade" id="runCaseForm" aria-hidden="true">
                    <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                    aria-hidden="true">×</span></button>
                            <h4 class="modal-title">用例运行</h4>
                        </div>
                        <div class="modal-body">
                            <div class="form-horizontal" method="post" action="">
                                <div class="form-group">
                                    <input type="hidden" id="debug_case_id" name="case_id">
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">请选择测试环境</label>
                                    <div class="col-sm-8">
                                        <select class="form-control m-b" id="debug_env_id" name="env_id">
                                        </select>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="col-sm-3 control-label"> 实际返回结果</label>
                                    <div class="col-sm-8"><textarea class="form-control" id="run_return_parameter"
                                                                    name="run_return_parameter"
                                                                    rows="5"></textarea>
                                    </div>
                                </div>

                                <div class="modal-footer">
                                    <button type="button" id="cancel_run" class="btn btn-default" data-dismiss="modal">
                                        取消
                                    </button>
                                    <button type="button" onclick="run_button()" class="btn btn-primary">调试
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                </div>
            </div>


        </div>
    </div>


    {% endblock %}

    {% block javascript %}
    <script>
        function debug_button(obj) {

            var case_id = obj.parentNode.parentNode.children[0].innerText;
            var prj_id = obj.parentNode.parentNode.children[2].getAttribute("prj_id");
            $("#debug_case_id").val(case_id);
            $("#cancel_run").html("取消");
            $("#cancel_run").attr("class", "btn btn-default");
            $("#run_return_parameter").val('');

            $.ajax({
                type: "get",
                url: "/base/findata/",
                data:
                    {
                        "prj_id": prj_id,
                        "type": "get_env_by_prj_id"
                    },
                success: function (data) {
                    var listdata = eval(data);
                    $(listdata).each(function (i, item) {
                        $("#debug_env_id").empty();
                        $("#debug_env_id").append("<option value=" + item.env_id + ">" + item.env_name + "</option>");
                    })
                },
                error: function () {
                    alert("获取测试环境失败！");
                }
            });
        };

        function run_button() {
            $.ajax({
                type: 'post',
                url: "/base/case_run/",
                data: {
                    "case_id": $("#debug_case_id").val(),
                    "env_id": $("#debug_env_id").val(),
                },
                success: function (res) {
                    $("#run_return_parameter").val(JSON.stringify(res));
                    //var res_data = eval("(" + res + ")");
                    //alert(res_data);
                    //$("#run_return_parameter").val(res_data.url + "\n" + res_data.header + "\n" + res_data.sendata + "\n" + res_data.revcdata);
                    $("#cancel_run").html("完成");
                    $("#cancel_run").attr("class", "btn btn-primary");
                },
                error: function () {
                    $("#run_return_parameter").val("调用接口失败!!!");
                    $("#cancel_run").html("完成");
                    $("#cancel_run").attr("class", "btn btn-danger");
                }
            });
        };
    </script>
    {% endblock %}